var b = [
    { id: 3, user_id: 2, path: "/category", path_name: "分类页面" },
    { id: 4, user_id: 2, path: "/good", path_name: "商品页面" },
    { id: 6, user_id: 2, path: "/order", path_name: "订单页面" }
]

var a = [
    {
        title: "商品管理",
        icon: "el-icon-s-tools",
        subs: [
            { title: "商品列表", path: "/good" },
            { title: "商品分类", path: "/category" },
            { title: "品牌管理", path: "/brand" },
            { title: "商品属性管理", path: "/attribute" },
        ],
    },
    {
        title: "订单中心",
        icon: "el-icon-s-management",
        subs: [{ title: "订单列表", path: "/order" }],
    },
    {
        title: "店铺运营",
        icon: "el-icon-service",
        subs: [
            { title: "专题管理", path: "/topic" },
            { title: "运营数据", path: "/statdata" },
        ],
    },
]

// let result = a.filter(item => {
//     let arr = []
//     let newArr = item.subs.filter(e => {
//         let subs = []
//         b.map(be => {
//             if (e.path == be.path) {
//                 subs.push(e)
//             }
//         })
//         return subs
//     })
//     if (newArr.length > 0) {
//         arr.push(newArr)
//     }
//     return arr
// })
// console.log(result)

// 老师教学
// 优化方案
var obj = {
    // "/category":true,
    // "/good":true,
    // "/order":true,
};
for (let n = 0; n < b.length; n++) {
    let path = b[n].path;
    obj[path] = true;
}

console.log(obj)

var c = [];
// 一级菜单
for (let i = 0; i < a.length; i++) {
    let subs = []; //存放新的二级菜单数据
    // 二级菜单
    for (let j = 0; j < a[i].subs.length; j++) {
        let sub = a[i].sub[j];
        // 循环用户权限列表  这里循环次数太多,可以优化
        // for (let k = 0; k > b.length; k++) {
        //     if (b[k].path === sub.path) {
        //         subs.push(sub)
        //     }
        // }
        // if (obj[sub.path]) {
        //     subs.push(sub)
        // }
        obj[sub.path] && subs.push(sub)
    }
    console.log(subs);
    if (subs.length > 0) {
        // c.push({
        //     title:a[i].title,
        //     icon:a[i].icon,
        //     subs:subs
        // })
        c.push({ ...a[i], subs: subs })
    }
}
